﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using LGE;

namespace MagicFight
{
    public class Ricochet : Magic
    {
        const int N = 8; // numero de projeteis
        int damage = Def.RicochetDef.Damage;

        Projectile[] projectile = new Projectile[N];
        Vector2[] directions = new Vector2[N] { Direction.Up, Direction.Down, Direction.Left, Direction.Right, Direction.Up, Direction.Down, Direction.Left, Direction.Right };

        public Ricochet(Character caster)
            : base(caster, Def.RicochetDef.N) { }

        public override void Effect(GameTime gameTime)
        {
            #region Cria projéteis
            // se o primeiro projectile é null, então ainda nenhum foi criado, ou seja, este é o primeiro update desta magia
            if (projectile[0] == null)
            {
                for (int i = 0; i < N; ++i)
                {
                    projectile[i] = new Projectile(caster, Engine.Load<Texture2D>("Images\\Magic\\fireball"), 3);
                    if (projectile[i].direction + directions[i] == Vector2.Zero) { float temp = projectile[i].direction.X; projectile[i].direction.X = projectile[i].direction.Y; projectile[i].direction.Y = temp; } // essa linha é bizarra mas serve pra corrigir a direção dos dois projéteis q vão para os lados do caster
                    else projectile[i].direction += directions[i];
                    if (i > 3) projectile[i].direction *= -1;
                    if (projectile[i].direction != Vector2.Zero) projectile[i].direction.Normalize();
                    projectile[i].position += projectile[i].direction * 55;
                    projectile[i].canCollideWithCaster = true;
                    projectile[i].canCollideWithProjectile = false;
                    projectile[i].velocity = 7f;
                    projectile[i].maxLifeTime = 7000;
                    projectile[i].ricochet = true;
                    ps.gameObjects.Add(projectile[i]);
                }
            }
            #endregion

            #region Checa se se todos projéteis estão vivos
            this.alive = false;
            for (int i = 0; i < N; ++i) if (projectile[i].alive) this.alive = true; // se algum projectile estiver vivo, a magia continua viva
            if (!this.alive) return;
            #endregion

            #region Checa se algum projétil colidiu
            for (int i = 0; i < N; ++i)
            {
                if (!projectile[i].alive || !projectile[i].collided) continue;

                // se chegou aqui significa aque acabou de colidir
                projectile[i].alive = false;
                if (projectile[i].target is Character)
                {
                    Character charTarget = (projectile[i].target as Character);
                    int d = charTarget.TakeDamage(damage, caster);
                    if (d > 0) ps.effectsManager.Add(new FloatingText(charTarget.position, d.ToString(), Color.Red));
                }
            }
            #endregion
        }
    }
}